<template>
  <el-dialog :title="title" :visible.sync="visible" width="500px" :before-close="handleClose">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px">
      <el-form-item label="学员：" prop="personnelId">
        <el-select v-model="ruleForm.personnelId" multiple collapse-tags placeholder="请选择" clearable filterable
          class="input-w300" @change="handleChange">
          <el-option v-for="item in userList" :key="item.id" :label="item.personnelName" :value="item.id">
            {{ item.personnelName }}{{ item.unitFullName ? '-' + item.unitFullName:'' }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="已选择学员：">
        {{ personNameList.join('、') }}
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleConfirm('ruleForm')">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addTrainingProjectPersonnel, updateTrainingSubject } from "@/api/subject";

export default {
  components: {},
  dicts: [],
  props: {
    visible: {
      default: false,
      type: Boolean
    },
    title: {
      default: '',
      type: String
    },
    editType: {
      default: 'add',
      type: String
    },
    editData: {
      default: {},
      type: Object
    },
    projectId: {
      default: '',
      type: String
    }
  },
  data () {
    return {
      userList: this.$store.state.user.userList,
      personNameList: [],
      ruleForm: {
        personnelId: [],
      },
      rules: {
        personnelId: [
          { required: true, message: '请选择学员', trigger: 'change' },
        ],
      }

    };
  },
  watch: {
    visible (val) {
      if (val) {
        if (this.editType === 'edit') {
          this.ruleForm = JSON.parse(JSON.stringify(this.editData))
          // ruleForm.personnelId
          this.personNameList=[]
        }
      } else {
        this.ruleForm = {
          personnelId: []
        }
        this.personNameList=[]
        this.resetForm('ruleForm')
      }
    }
  },
  methods: {
    handleChange (val) {
      this.personNameList = []
      val.map(item => {
        let obj = this.userList.find(userItem => {
          return userItem.id === item
        })
        this.personNameList.push(obj.personnelName)
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    },
    // 确定
    handleConfirm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.ruleForm.trainingProjectId = this.projectId
          const res = await addTrainingProjectPersonnel(this.ruleForm)
          if (res.code === 200) {
            this.$message.success('保存成功');
            this.handleClose()
          }
        }
      });
    },
    // 取消
    handleCancel () {
      this.$emit('update:visible', false)
    },
    // 关闭
    handleClose () {
      this.$emit('update:visible', false)
      this.$emit('close')
    }
  }
};
</script>
<style lang='scss' scoped></style>
